<?php
/**
 * PsiFramework
 *
 * Php Simple Framework
 *
 * PHP version 5.3
 *
 * @category  PsiFramework.Plugins.Form.Classes.Model
 * @package   PsiFramework.Plugins.Form.Classes.Model
 * @author    Remi San <remi.san@gmail.com>
 * @copyright 2011 Remi San
 * @license   http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @version   SVN: <svn_id>
 * @link      http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins\Form\Classes\Model\FormField.html
 */

namespace PsiFramework\Plugins\Form\Classes\Model;

import("Classes.Model.Object");

use \PsiFramework\Classes\Model\Object;
use \PsiFramework\Classes\Util\Object\Json;
use \PsiFramework\Plugins\Form\Classes\Util\Json\JsonForm;

/**
 * The FormField Object
 *
 * This is the class for the FormField Object
 *
 * @category PsiFramework.Plugins.Form.Classes.Model
 * @package  PsiFramework.Plugins.Form.Classes.Model
 * @author   Remi San <remi.san@gmail.com>
 * @license  http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @link     http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins\Form\Classes\Model\FormField.html
 */
class FormField extends Object
{

    /**
     * @var int
     */
    protected /*int*/             $_id ;
    /**
     * @var string
     */
    protected /*string*/          $_name ;
    /**
     * @var FieldType
     */
    protected /*FieldType*/       $_type ;
    /**
     * @var Json
     */
    protected /*Json*/            $_values ;
    /**
     * @var array
     */
    protected /*FormFieldLink[]*/ $_forms ;

    /**
     * Constructor
     * 
     * @param int       $id     The id
     * @param string    $name   The name
     * @param FieldType $type   The field type
     * @param Json      $values The values
     * @param array     $forms  The forms
     * 
     * @return FormField
     */
    public function __construct(
        /*int*/             $id=null,
        /*string*/          $name=null,
        /*FieldType*/       $type=null,
        Json                $values=null,
        /*FormFieldLink[]*/ $forms=null
    )
    {
        $this->_id = $id ;
        $this->_name = $name ;
        $this->_type = $type ;
        $this->_values = $values ;
        $this->_forms = $forms ;
    }

    /**
     * Getter for the id / primary key
     *
     * @see Object::getId()
     *
     * @return int
     */
    public function getId()
    {
        return $this->_id ;
    }

    /**
     * Setter for the id
     *
     * @param int $id The id
     *
     * @return void
     */
    public function setId(/*int*/ $id)
    {
        $this->_id = $id ;
    }

    /**
     * Getter for the name
     *
     * @return string
     */
    public function getName()
    {
        return $this->_name ;
    }

    /**
     * Setter for the name
     *
     * @param string $name The name
     *
     * @return void
     */
    public function setName(/*string*/ $name)
    {
        $this->_name = $name ;
    }

    /**
     * Getter for the type
     *
     * @return FieldType
     */
    public function getType()
    {
        return $this->_type ;
    }

    /**
     * Setter for the type
     *
     * @param FieldType $type The type
     *
     * @return void
     */
    public function setType(/*FieldType*/ $type)
    {
        $this->_type = $type ;
    }

    /**
     * Getter for the values
     *
     * @return Json
     */
    public function getValues()
    {
        return $this->_values ;
    }

    /**
     * Setter for the values
     *
     * @param Json $values The values
     *
     * @return void
     */
    public function setValues(Json $values)
    {
        $this->_values = $values ;
    }

    /**
     * The function to retrieve the structure
     *
     * @return JsonForm
     */
    public function getStructure()
    {
        $jsonText = $this->_type->getStructure()->serialize();
        $varNames = $this->_type->getFieldsDescriptors();

        foreach ($varNames as $varName) {
            $varValue = '' ;
            if (isset($this->_values->getObject()->{$varName})) {
                $varValue = $this->_values->getObject()->{$varName} ;
            }
            $jsonText = str_replace('<'.$varName.'>', $varValue, $jsonText);
        }

        $jsonForm = new JsonForm();
        $jsonForm->unserialize($jsonText);
        return $jsonForm ;
    }

    /**
     * Getter for the forms
     *
     * @return array
     */
    public function getForms()
    {
        return $this->_forms ;
    }

    /**
     * Setter for the forms
     *
     * @param array $forms The forms
     *
     * @return void
     */
    public function setForms(/*FormFieldLink[]*/ $forms)
    {
        $this->_forms = $forms ;
    }

    /**
     * Shows the FormField as a string
     *
     * @see Object::toString()
     *
     * @return string
     */
    public function toString()
    {
        return $this->_name ;
    }

}
